﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using RoketPack.Collections;
using System.IO;

namespace RoketPack
{
    class GlobalSettings
    {
        private string p_IMUsername = null;
        private string p_IMPassword = null;
        private RoketPack.IM.NotificationSettings p_IMNotifications = new RoketPack.IM.NotificationSettings();

        private GlobalSettings()
        {
        }

        /// <summary>
        /// Loads the global settings from file.
        /// </summary>
        /// <returns>A GlobalSettings object representing the global settings.</returns>
        static internal GlobalSettings Load()
        {
            GlobalSettings s = new GlobalSettings();
            Tree t = null;
            XmlReader reader = null;
            try
            {
                reader = XmlReader.Create("Settings.xml");
                t = Tree.FromXml(reader);
            }
            finally
            {
                reader.Close();
            }

            // Check for root element.
            Node rn = t.GetChildElement("settings");
            if (rn == null)
                throw new XmlException("The Settings.xml document does not contain a root <settings> node.");

            // Check for IM subelement.
            rn = rn.GetChildElement("im");
            if (rn != null)
            {
                if (rn.GetChildElement("username") != null && rn.GetChildElement("username").GetText() != null)
                    s.IMUsername = rn.GetChildElement("username").GetText().Value;
                if (rn.GetChildElement("password") != null && rn.GetChildElement("password").GetText() != null)
                    s.IMPassword = rn.GetChildElement("password").GetText().Value;
                if (rn.GetChildElement("notifications") != null)
                {
                    Node nn = rn.GetChildElement("notifications");

                    if (nn.GetChildElement("contactsignin") != null && nn.GetChildElement("contactsignin").GetText() != null)
                        s.IMNotifications.OnContactSignIn = (nn.GetChildElement("contactsignin").GetText().Value.ToLowerInvariant() == "true");
                    if (nn.GetChildElement("contactstartgame") != null && nn.GetChildElement("contactstartgame").GetText() != null)
                        s.IMNotifications.OnContactStartGame = (nn.GetChildElement("contactstartgame").GetText().Value.ToLowerInvariant() == "true");
                    if (nn.GetChildElement("contactjoinserver") != null && nn.GetChildElement("contactjoinserver").GetText() != null)
                        s.IMNotifications.OnContactJoinServer = (nn.GetChildElement("contactjoinserver").GetText().Value.ToLowerInvariant() == "true");
                    if (nn.GetChildElement("userstartgame") != null && nn.GetChildElement("userstartgame").GetText() != null)
                        s.IMNotifications.OnUserStartGame = (nn.GetChildElement("userstartgame").GetText().Value.ToLowerInvariant() == "true");
                    if (nn.GetChildElement("userjoinserver") != null && nn.GetChildElement("userjoinserver").GetText() != null)
                        s.IMNotifications.OnUserJoinServer = (nn.GetChildElement("userjoinserver").GetText().Value.ToLowerInvariant() == "true");
                }
            }

            // Return the new GlobalSettings object.
            return s;
        }

        /// <summary>
        /// Saves the global settings to a file.
        /// </summary>
        internal void Save()
        {
            Tree t = Tree.FromEmpty();
            Node n = null;

            // Add the root element.
            Node root = new Node(t, "element", "settings", null);
            t.Children.Add(root);

            // Add the <im> element.
            Node im = new Node(root, "element", "im", null);
            root.Children.Add(im);

            // Add the <username> and <password> elements.
            n = new Node(im, "element", "username", null);
            n.Children.Add(new Node(n, "text", null, this.IMUsername));
            im.Children.Add(n);
            n = new Node(im, "element", "password", null);
            n.Children.Add(new Node(n, "text", null, this.IMPassword));
            im.Children.Add(n);

            // Add the <notifications> element and subelements.
            Node nn = new Node(im, "element", "notifications", null);
            n = new Node(nn, "element", "contactsignin", null);
            n.Children.Add(new Node(n, "text", null, (this.IMNotifications.OnContactSignIn ? "true" : "false")));
            nn.Children.Add(n);
            n = new Node(nn, "element", "contactstartgame", null);
            n.Children.Add(new Node(n, "text", null, (this.IMNotifications.OnContactStartGame ? "true" : "false")));
            nn.Children.Add(n);
            n = new Node(nn, "element", "contactjoinserver", null);
            n.Children.Add(new Node(n, "text", null, (this.IMNotifications.OnContactJoinServer ? "true" : "false")));
            nn.Children.Add(n);
            n = new Node(nn, "element", "userstartgame", null);
            n.Children.Add(new Node(n, "text", null, (this.IMNotifications.OnUserStartGame ? "true" : "false")));
            nn.Children.Add(n);
            n = new Node(nn, "element", "userjoinserver", null);
            n.Children.Add(new Node(n, "text", null, (this.IMNotifications.OnUserJoinServer ? "true" : "false")));
            nn.Children.Add(n);

            // Write the data to Settings.xml file.
            TextWriter writer = null;
            try
            {
                writer = new StreamWriter("Settings.xml");
                writer.Write(t.ToXml());
            }
            finally
            {
                writer.Close();
            }
        }

        internal string IMUsername
        {
            get
            {
                return this.p_IMUsername;
            }
            set
            {
                this.p_IMUsername = value;
            }
        }

        internal string IMPassword
        {
            get
            {
                return this.p_IMPassword;
            }
            set
            {
                this.p_IMPassword = value;
            }
        }

        internal RoketPack.IM.NotificationSettings IMNotifications
        {
            get
            {
                return this.p_IMNotifications;
            }
            set
            {
                this.p_IMNotifications = value;
            }
        }
    }
}
